package com.fr.base.core.json;

import com.fr.report.script.core.FArray;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class JSONObject
{
  private HashMap myHashMap;
  public static final Object NULL = new Null(null);

  public JSONObject()
  {
    this.myHashMap = new HashMap();
  }

  public JSONObject(JSONObject paramJSONObject, String[] paramArrayOfString)
    throws JSONException
  {
    for (int i = 0; i < paramArrayOfString.length; ++i)
      putOpt(paramArrayOfString[i], paramJSONObject.opt(paramArrayOfString[i]));
  }

  public JSONObject(JSONTokener paramJSONTokener)
    throws JSONException
  {
    if (paramJSONTokener.nextClean() != '{')
      throw paramJSONTokener.syntaxError("A JSONObject text must begin with '{'");
    while (true)
    {
      int i = paramJSONTokener.nextClean();
      switch (i)
      {
      case 0:
        throw paramJSONTokener.syntaxError("A JSONObject text must end with '}'");
      case 125:
        return;
      }
      paramJSONTokener.back();
      String str = paramJSONTokener.nextValue().toString();
      i = paramJSONTokener.nextClean();
      if (i == 61)
        if (paramJSONTokener.next() != '>')
          paramJSONTokener.back();
      else if (i != 58)
        throw paramJSONTokener.syntaxError("Expected a ':' after a key");
      put(str, JSONUtils.objectDecode(paramJSONTokener.nextValue()));
      switch (paramJSONTokener.nextClean())
      {
      case ',':
      case ';':
        if (paramJSONTokener.nextClean() == '}')
          return;
        paramJSONTokener.back();
      case '}':
      }
    }
    return;
    throw paramJSONTokener.syntaxError("Expected a ',' or '}'");
  }

  public JSONObject(Map paramMap)
  {
    this.myHashMap = new HashMap(paramMap);
  }

  public JSONObject(Object paramObject)
  {
    Class localClass = paramObject.getClass();
    Method[] arrayOfMethod = localClass.getMethods();
    for (int i = 0; i < arrayOfMethod.length; ++i)
      try
      {
        Method localMethod = arrayOfMethod[i];
        String str1 = localMethod.getName();
        String str2 = "";
        if (str1.startsWith("get"))
          str2 = str1.substring(3);
        else if (str1.startsWith("is"))
          str2 = str1.substring(2);
        if ((str2.length() > 0) && (Character.isUpperCase(str2.charAt(0))) && (localMethod.getParameterTypes().length == 0))
        {
          if (str2.length() == 1)
            str2 = str2.toLowerCase();
          else if (!(Character.isUpperCase(str2.charAt(1))))
            str2 = str2.substring(0, 1).toLowerCase() + str2.substring(1);
          put(str2, localMethod.invoke(paramObject, null));
        }
      }
      catch (Exception localException)
      {
      }
  }

  public JSONObject(Object paramObject, String[] paramArrayOfString)
  {
    Class localClass = paramObject.getClass();
    for (int i = 0; i < paramArrayOfString.length; ++i)
    {
      String str = paramArrayOfString[i];
      try
      {
        Field localField = localClass.getField(str);
        Object localObject = localField.get(paramObject);
        put(str, localObject);
      }
      catch (Exception localException)
      {
      }
    }
  }

  public JSONObject(String paramString)
    throws JSONException
  {
    this(new JSONTokener(paramString));
  }

  public JSONObject accumulate(String paramString, Object paramObject)
    throws JSONException
  {
    testValidity(paramObject);
    Object localObject = opt(paramString);
    if (localObject == null)
      put(paramString, (paramObject instanceof JSONArray) ? new JSONArray().put(paramObject) : paramObject);
    else if (localObject instanceof JSONArray)
      ((JSONArray)localObject).put(paramObject);
    else
      put(paramString, new JSONArray().put(localObject).put(paramObject));
    return this;
  }

  public JSONObject append(String paramString, Object paramObject)
    throws JSONException
  {
    testValidity(paramObject);
    Object localObject = opt(paramString);
    if (localObject == null)
      put(paramString, new JSONArray().put(paramObject));
    else if (localObject instanceof JSONArray)
      put(paramString, ((JSONArray)localObject).put(paramObject));
    else
      throw new JSONException("JSONObject[" + paramString + "] is not a JSONArray.");
    return this;
  }

  public static String doubleToString(double paramDouble)
  {
    if ((Double.isInfinite(paramDouble)) || (Double.isNaN(paramDouble)))
      return "null";
    String str = Double.toString(paramDouble);
    if ((str.indexOf(46) > 0) && (str.indexOf(101) < 0) && (str.indexOf(69) < 0))
    {
      while (str.endsWith("0"))
        str = str.substring(0, str.length() - 1);
      if (str.endsWith("."))
        str = str.substring(0, str.length() - 1);
    }
    return str;
  }

  public Object get(String paramString)
    throws JSONException
  {
    Object localObject = opt(paramString);
    if (localObject == null)
      throw new JSONException("JSONObject[" + quote(paramString) + "] not found.");
    return localObject;
  }

  public boolean getBoolean(String paramString)
    throws JSONException
  {
    Object localObject = get(paramString);
    if ((localObject.equals(Boolean.FALSE)) || ((localObject instanceof String) && (((String)localObject).equalsIgnoreCase("false"))))
      return false;
    if ((localObject.equals(Boolean.TRUE)) || ((localObject instanceof String) && (((String)localObject).equalsIgnoreCase("true"))))
      return true;
    throw new JSONException("JSONObject[" + quote(paramString) + "] is not a Boolean.");
  }

  public double getDouble(String paramString)
    throws JSONException
  {
    Object localObject = get(paramString);
    try
    {
      return ((localObject instanceof Number) ? ((Number)localObject).doubleValue() : Double.valueOf((String)localObject).doubleValue());
    }
    catch (Exception localException)
    {
      throw new JSONException("JSONObject[" + quote(paramString) + "] is not a number.");
    }
  }

  public int getInt(String paramString)
    throws JSONException
  {
    Object localObject = get(paramString);
    return ((localObject instanceof Number) ? ((Number)localObject).intValue() : (int)getDouble(paramString));
  }

  public JSONArray getJSONArray(String paramString)
    throws JSONException
  {
    Object localObject = get(paramString);
    if (localObject instanceof JSONArray)
      return ((JSONArray)localObject);
    throw new JSONException("JSONObject[" + quote(paramString) + "] is not a JSONArray.");
  }

  public JSONObject getJSONObject(String paramString)
    throws JSONException
  {
    Object localObject = get(paramString);
    if (localObject instanceof JSONObject)
      return ((JSONObject)localObject);
    throw new JSONException("JSONObject[" + quote(paramString) + "] is not a JSONObject.");
  }

  public long getLong(String paramString)
    throws JSONException
  {
    Object localObject = get(paramString);
    return ((localObject instanceof Number) ? ((Number)localObject).longValue() : ()getDouble(paramString));
  }

  public static String[] getNames(JSONObject paramJSONObject)
  {
    int i = paramJSONObject.length();
    if (i == 0)
      return new String[0];
    Iterator localIterator = paramJSONObject.keys();
    String[] arrayOfString = new String[i];
    for (int j = 0; localIterator.hasNext(); ++j)
      arrayOfString[j] = ((String)localIterator.next());
    return arrayOfString;
  }

  public static String[] getNames(Object paramObject)
  {
    if (paramObject == null)
      return new String[0];
    Class localClass = paramObject.getClass();
    Field[] arrayOfField = localClass.getFields();
    int i = arrayOfField.length;
    if (i == 0)
      return new String[0];
    String[] arrayOfString = new String[i];
    for (int j = 0; j < i; ++j)
      arrayOfString[j] = arrayOfField[j].getName();
    return arrayOfString;
  }

  public String getString(String paramString)
    throws JSONException
  {
    return get(paramString).toString();
  }

  public boolean has(String paramString)
  {
    return this.myHashMap.containsKey(paramString);
  }

  public boolean isNull(String paramString)
  {
    return NULL.equals(opt(paramString));
  }

  public Iterator keys()
  {
    return this.myHashMap.keySet().iterator();
  }

  public int length()
  {
    return this.myHashMap.size();
  }

  public JSONArray names()
  {
    JSONArray localJSONArray = new JSONArray();
    Iterator localIterator = keys();
    while (localIterator.hasNext())
      localJSONArray.put(localIterator.next());
    return ((localJSONArray.length() == 0) ? null : localJSONArray);
  }

  public static String farrayToString(FArray paramFArray)
    throws JSONException
  {
    if (paramFArray == null)
      throw new JSONException("Null pointer");
    JSONArray localJSONArray = new JSONArray();
    for (int i = 0; i < paramFArray.length(); ++i)
      localJSONArray.put(paramFArray.elementAt(i));
    return localJSONArray.toString();
  }

  public static String date2String(Date paramDate)
    throws JSONException
  {
    if (paramDate == null)
      throw new JSONException("Null pointer");
    return "{\"__time__\":" + paramDate.getTime() + "}";
  }

  public static String numberToString(Number paramNumber)
    throws JSONException
  {
    if (paramNumber == null)
      throw new JSONException("Null pointer");
    String str = paramNumber.toString();
    if ((str.indexOf(46) > 0) && (str.indexOf(101) < 0) && (str.indexOf(69) < 0))
    {
      while (str.endsWith("0"))
        str = str.substring(0, str.length() - 1);
      if (str.endsWith("."))
        str = str.substring(0, str.length() - 1);
    }
    return str;
  }

  public Object opt(String paramString)
  {
    return ((paramString == null) ? null : this.myHashMap.get(paramString));
  }

  public boolean optBoolean(String paramString)
  {
    return optBoolean(paramString, false);
  }

  public boolean optBoolean(String paramString, boolean paramBoolean)
  {
    try
    {
      return getBoolean(paramString);
    }
    catch (Exception localException)
    {
    }
    return paramBoolean;
  }

  public JSONObject put(String paramString, Collection paramCollection)
    throws JSONException
  {
    put(paramString, new JSONArray(paramCollection));
    return this;
  }

  public double optDouble(String paramString)
  {
    return optDouble(paramString, (0.0D / 0.0D));
  }

  public double optDouble(String paramString, double paramDouble)
  {
    Object localObject;
    try
    {
      localObject = opt(paramString);
      return ((localObject instanceof Number) ? ((Number)localObject).doubleValue() : Double.parseDouble((String)localObject));
    }
    catch (Exception localException)
    {
    }
    return paramDouble;
  }

  public int optInt(String paramString)
  {
    return optInt(paramString, 0);
  }

  public int optInt(String paramString, int paramInt)
  {
    try
    {
      return getInt(paramString);
    }
    catch (Exception localException)
    {
    }
    return paramInt;
  }

  public JSONArray optJSONArray(String paramString)
  {
    Object localObject = opt(paramString);
    return ((localObject instanceof JSONArray) ? (JSONArray)localObject : null);
  }

  public JSONObject optJSONObject(String paramString)
  {
    Object localObject = opt(paramString);
    return ((localObject instanceof JSONObject) ? (JSONObject)localObject : null);
  }

  public long optLong(String paramString)
  {
    return optLong(paramString, 0L);
  }

  public long optLong(String paramString, long paramLong)
  {
    try
    {
      return getLong(paramString);
    }
    catch (Exception localException)
    {
    }
    return paramLong;
  }

  public String optString(String paramString)
  {
    return optString(paramString, "");
  }

  public String optString(String paramString1, String paramString2)
  {
    Object localObject = opt(paramString1);
    return ((localObject != null) ? localObject.toString() : paramString2);
  }

  public JSONObject put(String paramString, boolean paramBoolean)
    throws JSONException
  {
    put(paramString, (paramBoolean) ? Boolean.TRUE : Boolean.FALSE);
    return this;
  }

  public JSONObject put(String paramString, double paramDouble)
    throws JSONException
  {
    put(paramString, new Double(paramDouble));
    return this;
  }

  public JSONObject put(String paramString, int paramInt)
    throws JSONException
  {
    put(paramString, new Integer(paramInt));
    return this;
  }

  public JSONObject put(String paramString, long paramLong)
    throws JSONException
  {
    put(paramString, new Long(paramLong));
    return this;
  }

  public JSONObject put(String paramString, Map paramMap)
    throws JSONException
  {
    put(paramString, new JSONObject(paramMap));
    return this;
  }

  public JSONObject put(String paramString, Object paramObject)
    throws JSONException
  {
    if (paramString == null)
      throw new JSONException("Null key.");
    if (paramObject != null)
    {
      testValidity(paramObject);
      this.myHashMap.put(paramString, paramObject);
    }
    else
    {
      remove(paramString);
    }
    return this;
  }

  public JSONObject putOpt(String paramString, Object paramObject)
    throws JSONException
  {
    if ((paramString != null) && (paramObject != null))
      put(paramString, paramObject);
    return this;
  }

  public static String quote(String paramString)
  {
    if ((paramString == null) || (paramString.length() == 0))
      return "\"\"";
    int j = 0;
    int l = paramString.length();
    StringBuffer localStringBuffer = new StringBuffer(l + 4);
    localStringBuffer.append('"');
    for (int k = 0; k < l; ++k)
    {
      int i = j;
      j = paramString.charAt(k);
      switch (j)
      {
      case 34:
      case 92:
        localStringBuffer.append('\\');
        localStringBuffer.append(j);
        break;
      case 47:
        if (i == 60)
          localStringBuffer.append('\\');
        localStringBuffer.append(j);
        break;
      case 8:
        localStringBuffer.append("\\b");
        break;
      case 9:
        localStringBuffer.append("\\t");
        break;
      case 10:
        localStringBuffer.append("\\n");
        break;
      case 12:
        localStringBuffer.append("\\f");
        break;
      case 13:
        localStringBuffer.append("\\r");
        break;
      default:
        if ((j < 32) || ((j >= 128) && (j < 160)) || ((j >= 8192) && (j < 8448)))
        {
          String str = "000" + Integer.toHexString(j);
          localStringBuffer.append("\\u" + str.substring(str.length() - 4));
        }
        else
        {
          localStringBuffer.append(j);
        }
      }
    }
    localStringBuffer.append('"');
    return localStringBuffer.toString();
  }

  public Object remove(String paramString)
  {
    return this.myHashMap.remove(paramString);
  }

  static void testValidity(Object paramObject)
    throws JSONException
  {
    if (paramObject != null)
    {
      if (paramObject instanceof Double)
      {
        if ((!(((Double)paramObject).isInfinite())) && (!(((Double)paramObject).isNaN())))
          return;
        throw new JSONException("JSON does not allow non-finite numbers.");
      }
      if ((paramObject instanceof Float) && (((((Float)paramObject).isInfinite()) || (((Float)paramObject).isNaN()))))
        throw new JSONException("JSON does not allow non-finite numbers.");
    }
  }

  public JSONArray toJSONArray(JSONArray paramJSONArray)
    throws JSONException
  {
    if ((paramJSONArray == null) || (paramJSONArray.length() == 0))
      return null;
    JSONArray localJSONArray = new JSONArray();
    for (int i = 0; i < paramJSONArray.length(); ++i)
      localJSONArray.put(opt(paramJSONArray.getString(i)));
    return localJSONArray;
  }

  public Map toMap()
  {
    HashMap localHashMap = new HashMap();
    localHashMap.putAll(this.myHashMap);
    return localHashMap;
  }

  public String toString()
  {
    Iterator localIterator;
    try
    {
      localIterator = keys();
      StringBuffer localStringBuffer = new StringBuffer("{");
      while (localIterator.hasNext())
      {
        if (localStringBuffer.length() > 1)
          localStringBuffer.append(',');
        Object localObject = localIterator.next();
        localStringBuffer.append(quote(localObject.toString()));
        localStringBuffer.append(':');
        localStringBuffer.append(valueToString(this.myHashMap.get(localObject)));
      }
      localStringBuffer.append('}');
      return localStringBuffer.toString();
    }
    catch (Exception localException)
    {
    }
    return null;
  }

  public String toString(int paramInt)
    throws JSONException
  {
    return toString(paramInt, 0);
  }

  String toString(int paramInt1, int paramInt2)
    throws JSONException
  {
    int i;
    Object localObject;
    int j = length();
    if (j == 0)
      return "{}";
    Iterator localIterator = keys();
    StringBuffer localStringBuffer = new StringBuffer("{");
    int k = paramInt2 + paramInt1;
    if (j == 1)
    {
      localObject = localIterator.next();
      localStringBuffer.append(quote(localObject.toString()));
      localStringBuffer.append(": ");
      localStringBuffer.append(valueToString(this.myHashMap.get(localObject), paramInt1, paramInt2));
    }
    else
    {
      while (localIterator.hasNext())
      {
        localObject = localIterator.next();
        if (localStringBuffer.length() > 1)
          localStringBuffer.append(",\n");
        else
          localStringBuffer.append('\n');
        for (i = 0; i < k; ++i)
          localStringBuffer.append(' ');
        localStringBuffer.append(quote(localObject.toString()));
        localStringBuffer.append(": ");
        localStringBuffer.append(valueToString(this.myHashMap.get(localObject), paramInt1, k));
      }
      if (localStringBuffer.length() > 1)
      {
        localStringBuffer.append('\n');
        for (i = 0; i < paramInt2; ++i)
          localStringBuffer.append(' ');
      }
    }
    localStringBuffer.append('}');
    return localStringBuffer.toString();
  }

  public static String valueToString(Object paramObject)
    throws JSONException
  {
    if (paramObject == null)
      return "null";
    if (paramObject instanceof JSONString)
    {
      String str;
      try
      {
        str = ((JSONString)paramObject).toJSONString();
      }
      catch (Exception localException)
      {
        throw new JSONException(localException);
      }
      if (str instanceof String)
        return ((String)str);
      throw new JSONException("Bad value from toJSONString: " + str);
    }
    if (paramObject instanceof Number)
      return numberToString((Number)paramObject);
    if ((paramObject instanceof Boolean) || (paramObject instanceof JSONObject) || (paramObject instanceof JSONArray))
      return paramObject.toString();
    if (paramObject instanceof Map)
      return new JSONObject((Map)paramObject).toString();
    if (paramObject instanceof Collection)
      return new JSONArray((Collection)paramObject).toString();
    if (paramObject.getClass().isArray())
      return new JSONArray(paramObject).toString();
    if (paramObject instanceof FArray)
      return farrayToString((FArray)paramObject);
    return quote(paramObject.toString());
  }

  static String valueToString(Object paramObject, int paramInt1, int paramInt2)
    throws JSONException
  {
    if (paramObject == null)
      return "null";
    try
    {
      if (paramObject instanceof JSONString)
      {
        String str = ((JSONString)paramObject).toJSONString();
        if (str instanceof String)
          return ((String)str);
      }
    }
    catch (Exception localException)
    {
    }
    if (paramObject instanceof Number)
      return numberToString((Number)paramObject);
    if (paramObject instanceof Boolean)
      return paramObject.toString();
    if (paramObject instanceof JSONObject)
      return ((JSONObject)paramObject).toString(paramInt1, paramInt2);
    if (paramObject instanceof JSONArray)
      return ((JSONArray)paramObject).toString(paramInt1, paramInt2);
    if (paramObject instanceof Map)
      return new JSONObject((Map)paramObject).toString(paramInt1, paramInt2);
    if (paramObject instanceof Collection)
      return new JSONArray((Collection)paramObject).toString(paramInt1, paramInt2);
    if (paramObject.getClass().isArray())
      return new JSONArray(paramObject).toString(paramInt1, paramInt2);
    return quote(paramObject.toString());
  }

  public Writer write(Writer paramWriter)
    throws JSONException
  {
    int i;
    try
    {
      i = 0;
      Iterator localIterator = keys();
      paramWriter.write(123);
      while (localIterator.hasNext())
      {
        if (i != 0)
          paramWriter.write(44);
        Object localObject1 = localIterator.next();
        paramWriter.write(quote(localObject1.toString()));
        paramWriter.write(58);
        Object localObject2 = this.myHashMap.get(localObject1);
        if (localObject2 instanceof JSONObject)
          ((JSONObject)localObject2).write(paramWriter);
        else if (localObject2 instanceof JSONArray)
          ((JSONArray)localObject2).write(paramWriter);
        else
          paramWriter.write(valueToString(localObject2));
        i = 1;
      }
      paramWriter.write(125);
      return paramWriter;
    }
    catch (IOException localIOException)
    {
      throw new JSONException(localIOException);
    }
  }

  private static final class Null
  {
    private Null()
    {
    }

    protected final Object clone()
    {
      return this;
    }

    public boolean equals(Object paramObject)
    {
      return ((paramObject == null) || (paramObject == this));
    }

    public String toString()
    {
      return "null";
    }

    Null(JSONObject.1 param1)
    {
    }
  }
}